/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the GPL License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guardianapis.api.serialization;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.guardianapis.api.client.GuardianConstants;
import com.guardianapis.api.domain.jaxb.Content;
import com.guardianapis.api.domain.jaxb.Search;
import com.guardianapis.api.domain.jaxb.Tags;
import com.guardianapis.api.exceptions.ConfigurationException;
import com.guardianapis.api.exceptions.ContentException;
import com.guardianapis.api.util.StreamLogger;

public class JaxbContentSerializer implements GuardianContentSerializer {

	/*******************************************************************************
	 * Constants
	 *******************************************************************************/

	private static Log log = LogFactory.getLog(JaxbContentSerializer.class);

	/*******************************************************************************
	 * Properties
	 *******************************************************************************/

	private Unmarshaller unmarshaller;
	
	/*******************************************************************************
	 * Constructors
	 *******************************************************************************/
	
	public JaxbContentSerializer() throws ConfigurationException {
		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(GuardianConstants.JAXB_DOMAIN_OBJECTS_PACKAGE);
			unmarshaller = jaxbContext.createUnmarshaller();
			log.info("Created JAXB deserializer");
		} catch (JAXBException e) {
			throw new ConfigurationException("Unable to configure JAXB deserializer: " + e.getMessage(),e);
		}
	}
	
	/*******************************************************************************
	 * Implementing GuardianContentSerializer
	 *******************************************************************************/

	public Tags deSerializeTags(InputStream pXmlTagsStream) throws ContentException {
		Tags allSubjectsResult;

		if (log.isDebugEnabled()) {
			log.debug("Tags stream: " + StreamLogger.getStreamCharacters(pXmlTagsStream,GuardianConstants.NUM_CHARACTERS_TO_LOG_FROM_STREAM));
		}

		try {
			allSubjectsResult = (Tags) ((JAXBElement<?>) this.unmarshaller.unmarshal(pXmlTagsStream)).getValue();
		} catch (JAXBException e) {
			throw new ContentException("Failed to deserialize all subjects result: " + e.getMessage(),e);
		} 
		return allSubjectsResult;
	}

	public Content deSerializeContent(InputStream pXmlContentStream) throws ContentException {
		Content contentResult;

		if (log.isDebugEnabled()) {
			log.debug("Content stream: " + StreamLogger.getStreamCharacters(pXmlContentStream, GuardianConstants.NUM_CHARACTERS_TO_LOG_FROM_STREAM));
		}

		try {
			contentResult = (Content) ((JAXBElement<?>) this.unmarshaller.unmarshal(pXmlContentStream)).getValue();
		} catch (JAXBException e) {
			throw new ContentException("Failed to deserialize content result: " + e.getMessage(),e);
		} 
		return contentResult;
	}

	public Search deSerializeSearch(InputStream pXmlSearchStream) throws ContentException {
		Search searchResult;
		
		if (log.isDebugEnabled()) {
			log.debug("Search stream: " + StreamLogger.getStreamCharacters(pXmlSearchStream, GuardianConstants.NUM_CHARACTERS_TO_LOG_FROM_STREAM));
		}

		try {
			searchResult = (Search) ((JAXBElement<?>) this.unmarshaller.unmarshal(pXmlSearchStream)).getValue();
		} catch (JAXBException e) {
			throw new ContentException("Failed to deserialize search result: " + e.getMessage(),e);
		} 
		return searchResult;
	}

}
